<template>
	<view class="study-course-page home-wrap" :style="{ 'background':'#fff'}">
		<shopro-navbar :isBack="true" :background="{'background':'#fff'}">
			<text slot="content" class="u-flex u-flex-center u-flex-items-center" style="color: #000; width: 85%; text-align: center;padding-left: 20rpx;height: 100%;">
				初级课程（CAAC）
			</text>
		</shopro-navbar>
		<!-- 视频播放器 -->
		<view class="video-section">
			<video
				class="video-player"
				:src="videoUrl"
				:poster="videoPoster"
				controls
				:currentTime="videoCurrentTime"
				:duration="videoDuration"
				@timeupdate="onTimeUpdate"
				@ended="onVideoEnded">
			</video>
		</view>

		<!-- 课程章节目录 -->
		<view class="catalog-section">
			<view v-for="(section, sIdx) in sections" :key="sIdx" class="catalog-block">
				<view class="catalog-header" @click="toggleSection(sIdx)">
					<text class="catalog-title">{{ sIdx + 1 }}. {{ section.title }}</text>
					<view class="picker-btn-down">
						<image :src="HTTP_IMG_URL()+'/static/images/arrow-down.png'" model="aspectFill" :class="section.open?'up':'down'"></image>
					</view>
				</view>
				<view v-show="section.open" class="catalog-lessons">
					<view
						v-for="(lesson, lIdx) in section.lessons"
						:key="lIdx"
						:class="['lesson-row', isActiveLesson(sIdx, lIdx) ? 'active' : '']"
						@click="selectLesson(sIdx, lIdx)">
						<view class="lesson-info">
							<text class="lesson-title">{{ lesson.title }}</text>
						</view>
						<view class="lesson-action">
							<view v-if="!isPurchased" class="trial-tag" @click.stop="onTrial(lesson)">可试看</view>
							<view v-else class="study-btn" @click.stop="onStudy(lesson)">开始学习</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 未购买弹框 -->
		<view v-if="showTrialModal" class="modal-mask">
			<view class="modal-content">
				<view class="modal-message">
					很抱歉，您尚未购买该课程，只能观看1分钟哦！需要观看完整版，请您购买吧！
				</view>
				<view class="modal-btn" @click="onBuy">立即购买</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			// 视频相关
			videoUrl: '', // TODO: 对接接口
			videoPoster: this.$imgUrl+'/static/images/video.png',
			videoCurrentTime: 0, // TODO: 对接进度
			videoDuration: 56, // TODO: 对接时长
			sections: [],
			activeSection: 0,
			activeLesson: 0,
			isPurchased: false, // TODO: 对接后端字段
			showTrialModal: false,
			trialLimit: 60, // 试看时长（秒）
			isTrialing: false, // 当前是否处于试看
		}
	},
	onLoad(options){
		const lessonObj = JSON.parse(options.lessonStr);
		this.videoUrl = lessonObj.video;
		console.log(options, 'optionsoptions');
	},
	methods: {
		HTTP_IMG_URL() {
			return this.$imgUrl;
		},
		onBack() {
			uni.navigateBack();
		},
		// 视频进度条相关
		onTimeUpdate(e) {
			this.videoCurrentTime = Math.floor(e.detail.currentTime);
			this.videoDuration = Math.floor(e.detail.duration);
			// 试看逻辑：未购买且处于试看，超时弹框
			if (!this.isPurchased && this.isTrialing && this.videoCurrentTime >= this.trialLimit) {
				this.showTrialModal = true;
				this.isTrialing = false;
				// 自动暂停视频
				const videoCtx = uni.createVideoContext('video-player', this);
				videoCtx.pause();
			}
		},
		onVideoEnded() {
			// TODO: 课程视频播放结束逻辑
		},
		formatTime(sec) {
			if (!sec && sec !== 0) return '00:00';
			const m = String(Math.floor(sec / 60)).padStart(2, '0');
			const s = String(Math.floor(sec % 60)).padStart(2, '0');
			return `${m}:${s}`;
		},
		// 章节目录交互
		toggleSection(idx) {
			this.sections[idx].open = !this.sections[idx].open;
			this.$forceUpdate();
		},
		selectLesson(sIdx, lIdx) {
			this.activeSection = sIdx;
			this.activeLesson = lIdx;
			// TODO: 选中小节后播放对应视频或跳转
		},
		isActiveLesson(sIdx, lIdx) {
			return this.activeSection === sIdx && this.activeLesson === lIdx;
		},
		onTrial(lesson) {
			// 试看逻辑：点击试看，重置视频到0，开始试看
			this.isTrialing = true;
			this.videoCurrentTime = 0;
			// 自动播放视频
			const videoCtx = uni.createVideoContext('video-player', this);
			videoCtx.seek(0);
			videoCtx.play();
		},
		onStudy(lesson) {
			// 已购买，直接学习
			uni.showToast({ title: '开始学习', icon: 'none' });
			// TODO: 跳转或播放完整视频
		},
		onBuy() {
			// 立即购买事件口子
			this.showTrialModal = false;
			uni.showToast({ title: '跳转购买页', icon: 'none' });
			// TODO: 跳转到购买页面
		},
		// 数据对接口子
		getCourseDetail() {
			// TODO: 拉取课程详情
		},
		getSectionList() {
			// TODO: 拉取章节列表
		}
	},
	mounted() {
		// TODO: 页面加载时拉取数据
		// this.getCourseDetail();
		// this.getSectionList();
	}
}
</script>

<style lang="scss" scoped>
.study-course-page {
	background: #f7f8fa;
	min-height: 100vh;
	padding-bottom: 20rpx;
}
.nav-bar {
	display: flex;
	align-items: center;
	height: 88rpx;
	padding: 0 24rpx;
	background: #fff;
	box-shadow: 0 2rpx 8rpx #f0f1f2;
}
.nav-title {
	flex: 1;
	text-align: center;
	font-size: 34rpx;
	font-weight: 500;
}
.video-section {
	position: relative;
	margin: 24rpx 24rpx 0 24rpx;
	border-radius: 20rpx;
	overflow: hidden;
	background: #000;
}
.video-player {
	width: 100%;
	height: 420rpx;
	background: #000;
}
.video-progress {
	position: absolute;
	left: 0;
	bottom: 0;
	width: 100%;
	display: flex;
	justify-content: flex-end;
	align-items: center;
	padding: 0 16rpx 8rpx 0;
}
.video-time {
	color: #fff;
	font-size: 22rpx;
	background: rgba(0,0,0,0.4);
	border-radius: 8rpx;
	padding: 2rpx 8rpx;
}
.catalog-section {
	margin: 24rpx 24rpx 0 24rpx;
}
.catalog-block {
	margin-bottom: 16rpx;
	border-bottom: 1rpx solid #f0f0f0;
}
.catalog-header {
	display: flex;
	justify-content: space-between;
	align-items: center;
	font-size: 28rpx;
	font-weight: normal;
	padding: 16rpx 0;
	cursor: pointer;
}
.catalog-title {
	color: #14161E;
}
.catalog-lessons {
	padding-left: 16rpx;
}
.lesson-row {
	display: flex;
	align-items: center;
	justify-content: space-between;
	background: #F5F4F9;
	border-radius: 12rpx;
	margin: 12rpx 0;
	padding: 20rpx 16rpx;
	border: 2rpx solid transparent;
}
.lesson-row.active {
	border: 2rpx solid #2563eb;
	background: #eaf1ff;
}
.lesson-title {
	font-size: 26rpx;
	font-weight: normal;
	color: #333333;
}
.active{
	.lesson-title{
		color: #2563eb;
	}
}
.lesson-action {
	display: flex;
	align-items: center;
}
.trial-tag {
	margin-left: 16rpx;
	padding: 0 16rpx;
	height: 40rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	border: 2rpx solid #2563eb;
	color: #2563eb;
	background: rgba(22,100,255,0.11);
	border-top-right-radius: 22rpx;
	border-top-left-radius: 22rpx;
	border-bottom-right-radius: 22rpx;
	font-size: 20rpx;
}
.study-btn {
	background: #2563eb;
	color: #fff;
	border-radius: 16rpx;
	font-size: 22rpx;
	padding: 4rpx 16rpx;
}

// 弹框样式
.modal-mask {
	position: fixed;
	left: 0; top: 0; right: 0; bottom: 0;
	background: rgba(0,0,0,0.35);
	z-index: 1000;
	display: flex;
	align-items: center;
	justify-content: center;
}
.modal-content {
	width: 600rpx;
	background: #fff;
	border-radius: 24rpx;
	overflow: hidden;
	box-shadow: 0 8rpx 32rpx rgba(0,0,0,0.10);
	display: flex;
	flex-direction: column;
	align-items: center;
	padding: 48rpx 32rpx 32rpx 32rpx;
}
.modal-message {
	color: #222;
	font-size: 30rpx;
	text-align: center;
	margin-bottom: 40rpx;
}
.modal-btn {
	color: #2563eb;
	font-size: 32rpx;
	font-weight: bold;
	text-align: center;
	width: 100%;
	padding: 20rpx 0 0 0;
}
.picker-btn-down{
	width: 20rpx;
	height: 14rpx;
	margin-left: 18rpx;
	image{
		width: 100%;
		height: 100%;
		margin-bottom: 20rpx ;
	}
}
.up{
	transform: rotate(180deg);
}
.down{
	transform: rotate(0deg);
}
</style>
